package com.ziytek.web.citizen.listener;

import com.google.gson.Gson;
import com.ziytek.web.citizen.config.caffeine.CaffeineUtil;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.ServerConstant;
import com.ziytek.web.citizen.event.OfflineEvent;
import com.ziytek.web.citizen.mapper.BasicCabinetIpMapper;
import com.ziytek.web.citizen.model.vo.StatusVo;
import com.ziytek.web.citizen.service.CabinetStatusPushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 设备离线
 *
 * @author fenghx
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OfflineEventListener {
    private final Gson gson;
    private final BasicCabinetIpMapper basicCabinetIpMapper;
    private final CabinetStatusPushService cabinetStatusPushService;

    @Async("myAsyncExecutor")
    @EventListener
    @Order(ServerConstant.DATA_TYPE_LEN_1)
    public void offlineEventListener(OfflineEvent event) {
        Assert.notNull(event, "event is null");
        Assert.hasLength(event.getIp(), "event ip is null");

        // 业务处理
        offline(event.getIp());

        cabinetStatusPushService.pushByIp(event.getIp(), false);
    }

    private void offline(String ip) {

        log.info("电池设备离线监听offlineEventListener: {}", ip);

        if (StringUtils.isBlank(ip)) {
            log.info("offlineEventListener failed ip is null: {}", ip);
            return;
        }

        var ips = basicCabinetIpMapper.queryCabinetIpInfoByIp(ip);
        if (null == ips || StringUtils.isBlank(ips.getCabinetNo())) {
            return;
        }

        var cab = ips.getCabinetNo();
        var key = "statusVo_key_" + Integer.parseInt(cab);

        StatusVo statusVo = CaffeineUtil.get(key);
        if (null != statusVo) {
            statusVo.setStatus("offline");
            log.info("[{}]设备离线statusVo:{}", key, gson.toJson(statusVo));
            boolean hasData = statusVo.getFinishCount() > NumConstant.INT_ZERO
                    || statusVo.getFaultsCount() > NumConstant.INT_ZERO
                    || statusVo.getRunsCount() > NumConstant.INT_ZERO
                    || statusVo.getWaitCount() > NumConstant.INT_ZERO;
            if (!hasData) {
                statusVo.setRunsCount(NumConstant.INT_ZERO);
                statusVo.setWaitCount(NumConstant.INT_ZERO);
                statusVo.setSpacesCount(NumConstant.INT_ZERO);
                statusVo.setFaultsCount(NumConstant.INT_ZERO);
                statusVo.setFinishCount(NumConstant.INT_ZERO);
            }
            CaffeineUtil.set(key, statusVo, 24 * 60 * 60);
        }

        //deviceOfflineService.deviceOffline(ip);
        // 延时十秒
        CaffeineUtil.set(CaffeineUtil.CACHE_KEY_DEVICE_OFFLINE_YS + ip, ip, 10);
        log.info("offlineEventListener delete cache success:{}-{}", cab, ip);
    }
}
